<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :search.sync="search"
      @row-del="rowDel"
      :before-open="beforeOpen"
      :page="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menuRight="{ row }">
        <el-button type="primary" size="small">批量删除</el-button>
        <el-button type="primary" size="small" @click="addRow"
          >新增优惠价</el-button
        >
      </template>
      <template #menuLeft>
        <el-input
          placeholder="请输入加注站名称或加注机编号"
          size="small"
          style="width: 250px"
          suffix-icon="el-icon-search"
        />
      </template>
    </avue-crud>
    <el-dialog
      title="新增优惠价"
      append-to-body
      :visible.sync="addDialog"
      width="600px"
    >
      <preferential-price-for-filling-machines-add
        @closeAddDialog="closeAddDialog"
      />
    </el-dialog>
  </basic-container>
</template>

<script>
import preferentialPriceForFillingMachinesAdd from "@/views/marketing-management/preferential-price-for-filling-machines-add.vue";
import { getList, remove } from "@/api/system/user";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      search: {},
      roleBox: false,
      excelBox: false,
      loading: true,
      selectionList: [],
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      props: {
        label: "title",
        value: "key",
      },
      option: {
        columnBtn: false,
        refreshBtn: false,
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        addBtn: false,
        dialogWidth: "800px",
        labelWidth: "150px",
        column: [
          {
            label: "加注机名称",
            span: 24,
            disabled: true,
          },
          {
            label: "加注机编号",
            span: 24,
            disabled: true,
          },
          {
            label: "优惠单价（元/kg~L）",
            span: 24,
          },
        ],
      },
      data: [],
      addDialog: false,
    };
  },
  components: {
    preferentialPriceForFillingMachinesAdd,
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_add, false),
        viewBtn: this.vaildData(this.permission.user_view, false),
        delBtn: this.vaildData(this.permission.user_delete, false),
        editBtn: this.vaildData(this.permission.user_edit, false),
      };
    },
  },
  methods: {
    closeAddDialog() {
      this.addDialog = false;
    },
    addRow() {
      this.addDialog = true;
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
